package com.dominator.controller.api;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.JsonUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.service.ApiMerchantService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.network.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
@RequestMapping("/api/v1/merchant")
public class ApiMerchantController {
    @Autowired
    HttpServletRequest request;

    @Autowired
    ApiMerchantService apiMerchantService;


    /**
     * 获取联合登录参数
     *
     * @return
     */
    @RequestMapping(value = "/getMerchantUnionLoginParams")
    @ResponseBody
    public ApiMessage getMerchantUnionLoginParams(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiMerchantService.getMerchantUnionLoginParams(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }



    /**
     * 商户查询余量
     *
     * @return
     */
    @RequestMapping(value = "/merchantLotteryLimit")
    @ResponseBody
    public ApiMessage merchantLotteryLimit(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiMerchantService.merchantLotteryLimit(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


    /**
     * 商户发出二维码彩票
     *
     * @return
     */
    @RequestMapping(value = "/merchantLotterySend")
    @ResponseBody
    public ApiMessage merchantLotterySend(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiMerchantService.merchantLotterySend(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 商户账户明细
     *
     * @return
     */
    @RequestMapping(value = "/getAccountInfo")
    @ResponseBody
    public ApiMessage getAccountInfo(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiMerchantService.getAccountInfo(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 商户转积分给用户
     *
     * @return
     */
    @RequestMapping(value = "/transferPointsToUser")
    @ResponseBody
    public ApiMessage transferPointsToUser(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiMerchantService.transferPointsToUser(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


    /**
     * 商户用户登录
     *
     * @return
     */
    @RequestMapping(value = "/merchantUserLogin")
    @ResponseBody
    public ApiMessage merchantUserLogin(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiMerchantService.merchantUserLogin(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

}
